package view;

import controller.*;
import javax.swing.JOptionPane;
import model.userhandler.*;

/**
 *
 * @author tobias
 */
public class LoginPanel extends javax.swing.JPanel {
    private Controller ctrl;
    private UserManager uManager;
    private User currentUser;

    /** Creates new form LoginPanel */
    public LoginPanel(Controller ctrl, UserManager uManager) {
        initComponents();
        this.ctrl = ctrl;
        this.uManager = uManager;
        updateUserList();
    }

    public void updateUserList() {
        userSelector.removeAllItems();
        if(uManager.getUsers().isEmpty()) {
            userSelector.setEnabled(false);
            passwordField.setEnabled(false);
            logInBtn.setEnabled(false);
            userAdminBtn.requestFocus();;
        } else {
            int userCounter = 0;
            for(User u: uManager.getUsers()) {
                userSelector.addItem(u);
                userCounter++;
            }
            userSelector.setEnabled(true);
            passwordField.setEnabled(true);
            if(userCounter == 1) {
                userSelector.setSelectedIndex(0);
                passwordField.requestFocus();
            } else {
            userSelector.requestFocus();
            }
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        logInBtn = new javax.swing.JButton();
        quitBtn = new javax.swing.JButton();
        userAdminBtn = new javax.swing.JButton();
        userSelector = new javax.swing.JComboBox();

        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+3));
        jLabel1.setText("Velkommen til LifeManager");

        jLabel2.setFont(jLabel2.getFont());
        jLabel2.setText("Indtast dine brugeroplysninger for at logge på");

        passwordField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordFieldActionPerformed(evt);
            }
        });

        jLabel3.setText("Bruger:");

        jLabel4.setText("Adgangskode:");

        logInBtn.setText("Log på");
        logInBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logInBtnActionPerformed(evt);
            }
        });

        quitBtn.setText("Afslut");
        quitBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitBtnActionPerformed(evt);
            }
        });

        userAdminBtn.setText("Brugeradministration");
        userAdminBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userAdminBtnActionPerformed(evt);
            }
        });

        userSelector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userSelectorActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(jLabel2))
                .add(92, 92, 92))
            .add(layout.createSequentialGroup()
                .add(55, 55, 55)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel3)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(userSelector, 0, 259, Short.MAX_VALUE)
                    .add(passwordField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 259, Short.MAX_VALUE))
                .add(57, 57, 57))
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(logInBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(userAdminBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(quitBtn, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2)
                .add(44, 44, 44)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(userSelector, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(passwordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 58, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(userAdminBtn)
                    .add(logInBtn)
                    .add(quitBtn))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void quitBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitBtnActionPerformed
        System.exit(0);
    }//GEN-LAST:event_quitBtnActionPerformed

    private void logInBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logInBtnActionPerformed
        String passEntered = String.valueOf(passwordField.getPassword());
        if(passEntered.equals(currentUser.getPassword())) {
            ctrl.logon(currentUser);
        } else {
            JOptionPane.showMessageDialog(ctrl.getFrame(), "Den adgangskode du har indtastet til brugeren er forkert!\nTip: Ved nyoprettede brugere skal passwordfeltet være blankt, med mindre du selv har ændret det.", "Adgang nægtet", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_logInBtnActionPerformed

    private void userSelectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userSelectorActionPerformed
        if(userSelector.getSelectedIndex() != -1) {
            currentUser = (User)userSelector.getSelectedItem();
            passwordField.requestFocus();
            logInBtn.setEnabled(true);
        }
    }//GEN-LAST:event_userSelectorActionPerformed

    private void userAdminBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userAdminBtnActionPerformed
        UserAdminDialog uad = new UserAdminDialog(ctrl.getFrame(), true, ctrl, this);
        uad.setVisible(true);
}//GEN-LAST:event_userAdminBtnActionPerformed

    private void passwordFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_passwordFieldActionPerformed
        if(userSelector.getSelectedIndex() != -1) {
            logInBtnActionPerformed(evt);
        }
    }//GEN-LAST:event_passwordFieldActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton logInBtn;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JButton quitBtn;
    private javax.swing.JButton userAdminBtn;
    private javax.swing.JComboBox userSelector;
    // End of variables declaration//GEN-END:variables

}
